/**
 * 自定义公共函数
 */
var myfn = {};

/**
 * 将img/video标签修改为video/img
 * @param {*} htmlstr 
 * @param {number} type 0 imgToVideo videoToImg 
 * vk.myfn.imgVideoTagTurn();
 */
myfn.imgVideoTagTurn = function (htmlstr,type){
	
	let newHtml = '';
	
	if(type == 0){
		
		newHtml = htmlstr.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/ig, function(match, thumbsrc) {
			
			let video_result = match;
			
			match.replace(/data-custom="*videoSrc=([^'"]+)[^>]*>/ig, function(match, src){
				
				let dataCustom = `data-custom="imgSrc=${thumbsrc}"`
										 
				video_result = '<video src="' + src + '" ' + dataCustom  + '></video>'
							
			})
			
			return video_result;
		});
		
	}else{
		
		newHtml = htmlstr.replace(/<video [^>]*src=['"]([^'"]+)[^>]*>/ig, function(match, thumbsrc) {
			
			let video_result = match;
			
			match.replace(/data-custom="*imgSrc=([^'"]+)[^>]*>/ig, function(match, src){
				
				let dataCustom = `data-custom="videoSrc=${thumbsrc}"`
										 
				video_result = '<img src="' + src + '" ' + dataCustom  + '/>'
							
			})
			
			return video_result;
		});
		
	}
			
	return newHtml;
	
}	

myfn.toolbarConfig = function (){
	
	return {
		insertKeys: {
		    index: 0, // 插入的位置，基于当前的 toolbarKeys
		    keys: ['AI']
		}
	}
	
}	

myfn.editorConfig = function (placeholder){
	
	return {
	    placeholder: placeholder,
	    MENU_CONF:{
	      uploadImage: {
			async customUpload(file, insertFn) {                  // JS 语法
				// res 即服务端的返回结果
				
				let objectURL = URL.createObjectURL(file);
												
				vk.callFunctionUtil.uploadFile({
					title:"上传中...",
					filePath: objectURL,
					fileType: "image",
					needSave:true,
					success(res) {
						console.log(res);
						let imageUrl = res.fileID;
						insertFn(imageUrl, "image", "")
					}
				});
								
			},
	      },
	      uploadVideo: {
	        async customUpload(file, insertFn) {                   // JS 语法
	            
				let suffix = file.name.split(".");
				
				let objectURL = URL.createObjectURL(file);
				
				// 上传视频到云储存
				vk.callFunctionUtil.uploadFile({
					title:"上传中...",
					filePath: objectURL,
					fileType: "video",
					needSave:true,
					suffix: suffix[1],
					success(res) {
						
						let videoUrl = res.fileID;
						insertFn(videoUrl)
						
					}
				});
			
	        }
	      }
	    }
	  }
	
}	


export default myfn;
